package com.backwaveweibo.bww.controller;

import com.backwaveweibo.bww.service.LikeService;
import com.backwaveweibo.bww.utils.JwtTokenUtil;
import com.backwaveweibo.bww.utils.Result;
import com.backwaveweibo.bww.utils.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

@RestController
@RequestMapping("like")
@Slf4j
public class LikeController {
    @Resource
    LikeService likeService;

    /**
     * 点赞接口传入被点赞的id
     * @param weiboId 被点赞的微博id
     * @return
     */
    @RequiresRoles(logical = Logical.OR,value = {"admin","user"})
    @PostMapping("setLike")
    public Result setLike(Long weiboId){

        if (weiboId != null && weiboId != 0) {
            Boolean likeWeibo = likeService.likeWeibo(Long.valueOf(weiboId), Objects.requireNonNull(JwtTokenUtil.getUser()).getUserId());
            return new Result().setData(likeWeibo).setMessage("点赞"+(likeWeibo?"成功":"失败")).setCode(likeWeibo?ResultCode.SUCCESS:ResultCode.FAIL);
        } else {
            return new Result().setMessage("点赞失败").setCode(ResultCode.FAIL);
        }
    }

    /**
     * 获取点赞数
     * @param weiboId 获取想要获取赞数的微博id
     * @return
     */
    @GetMapping("getLikeCount")
    public Result getLikeCount(Long weiboId){
        if (weiboId != null && weiboId != 0){
            Long likeCount = likeService.getLikeCount(weiboId);
            return new Result().setData(likeCount).setMessage("获取点赞数成功").setCode(ResultCode.SUCCESS);
        }else {
            return new Result().setMessage("获取点赞数失败").setCode(ResultCode.FAIL);
        }

    }

    /**
     *点踩
     * @param weiboId 想要点踩的微博id
     * @return
     */
    @RequiresRoles(logical = Logical.OR,value = {"admin","user"})
    @PostMapping("setDislike")
    public Result setDislike(Long weiboId){
        if (weiboId != null && weiboId != 0) {
            Boolean disLikeWeibo = likeService.disLikeWeibo(weiboId, Objects.requireNonNull(JwtTokenUtil.getUser()).getUserId());
            return new Result().setData(disLikeWeibo).setMessage("点不攒"+(disLikeWeibo?"成功":"失败")).setCode(disLikeWeibo?ResultCode.SUCCESS:ResultCode.FAIL);
        } else {
            return new Result().setMessage("点不赞失败").setCode(ResultCode.FAIL);
        }
    }

    /**
     * 获取点踩的数量
     * @param weiboId 想要点踩的微博id
     * @return
     */
    @GetMapping("getDislikeCount")
    public Result getDislikeCount(Long weiboId){
        if (weiboId != null && weiboId != 0) {
            Long dislikeWeiboCount = likeService.getDislikeWeiboCount(weiboId);
            return new Result().setData(dislikeWeiboCount).setMessage("获取踩数成功").setCode(ResultCode.SUCCESS);
        } else {
            return new Result().setMessage("获取踩数失败").setCode(ResultCode.FAIL);
        }
    }

    /**
     * 用户是否点赞
     * @param weiboId
     * @return
     */
    @GetMapping("isLike")
    public Result isLike(Long weiboId){
        if (weiboId != null && weiboId != 0) {
            Boolean like = likeService.isLike(weiboId, Objects.requireNonNull(JwtTokenUtil.getUser()).getUserId());
            return new Result().setData(like).setCode(ResultCode.SUCCESS).setMessage("获取成功");
        } else {
            return new Result().setMessage("获取是否点赞失败").setCode(ResultCode.FAIL);
        }
    }

    /**
     * 用户是否点踩
     * @param weiboId
     * @return
     */
    @GetMapping("isDislike")
    public Result isDislike(Long weiboId){
        if (weiboId != null && weiboId != 0) {
            Boolean like = likeService.isDislike(weiboId, Objects.requireNonNull(JwtTokenUtil.getUser()).getUserId());
            return new Result().setData(like).setCode(ResultCode.SUCCESS).setMessage("获取成功");
        } else {
            return new Result().setMessage("获取是否不喜欢失败").setCode(ResultCode.FAIL);
        }
    }
}
